package com.ruibo.tax.service.impl;

import com.ruibo.tax.model.SWRNSSB;
import com.ruibo.tax.model.base.PackageHead;
import com.ruibo.tax.model.log.TaxDataLog;
import com.ruibo.tax.model.log.TaxLog;
import com.ruibo.tax.model.log.TaxReceiveLog;
import com.ruibo.tax.model.xml2bean.SWRNSSBXMLEncodeModel;
import com.ruibo.tax.mq.send.ReconciliationSendZIPMessage;
import com.ruibo.tax.repository.SWRNSSBRepository;
import com.ruibo.tax.repository.TaxReceiveLogRepository;
import com.ruibo.tax.service.SWRNSSBService;
import com.ruibo.tax.service.TaxDataLogService;
import com.ruibo.tax.service.TaxLogService;
import com.ruibo.tax.utils.XMLUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Zhang Jibing
 * @version 1.0
 * @date 2020/10/15 16:07
 * @company Jinan RuiBo Information Technology Co., Ltd
 * @address Jinan, China
 */
@Service
public class SWRNSSBServiceImpl implements SWRNSSBService {
    @Autowired
    private SWRNSSBRepository swrnssbRepository;

    @Autowired
    private TaxLogService taxLogService;

    @Autowired
    private TaxDataLogService taxDataLogService;

    @Autowired
    private TaxReceiveLogRepository taxReceiveLogRepository;

    @Override
    @Transactional
    public void processingData(String data, PackageHead heads) {
        try {
            JAXBContext jaxbContext1 = JAXBContext.newInstance(SWRNSSBXMLEncodeModel.class);
            Unmarshaller unmarshaller = jaxbContext1.createUnmarshaller();
            StringReader stringReader1 = new StringReader(data);
            SWRNSSBXMLEncodeModel swrnssbxmlEncodeModel = (SWRNSSBXMLEncodeModel) unmarshaller.unmarshal(stringReader1);
            List<SWRNSSB> roots = swrnssbxmlEncodeModel.getRoots();
            swrnssbRepository.saveAll(roots);
            // 接受税务数据日志  实时的包
            TaxLog taxLog = taxLogService.handlerData(heads);
            roots.forEach(fhtqxx -> {
                TaxDataLog taxDataLog = new TaxDataLog();
                taxDataLog.setDataId(fhtqxx.getId());
                taxDataLog.setLogId(taxLog.getId());
                taxDataLog.setCreateAt(LocalDateTime.now());
                //汇总的单条
                taxDataLogService.save(taxDataLog);
            });
            roots.forEach(fhtqxx -> {
                TaxReceiveLog taxReceiveLog = new TaxReceiveLog();
                taxReceiveLog.setSJBBH(heads.getSJBBH());
                taxReceiveLog.setSJBLX(heads.getSJBLX());
                taxReceiveLog.setUUID(fhtqxx.getUUID());
                taxReceiveLog.setUpdateAt(LocalDateTime.now());
                taxReceiveLog.setCreateAt(LocalDateTime.now());
                //汇总的单条
                taxReceiveLogRepository.save(taxReceiveLog);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
